/**
 * Classe permettant l'affichage graphique de la fenêtre statistique ainsi que les écouteurs d'évènements sur les boutons concernés.
 * @authors:  BRENET Victor , SARRA Fabrice
 * @version 1.0
 */
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Integer;
import javax.swing.event.*;

public class EventSimulationFrame extends JFrame implements ActionListener, WindowListener, ChangeListener
{

	private Building building;
	private Vector<Customer> customers = new Vector<Customer>();

	private JPanel topestPanel;
	private JPanel topPanel;
	private JPanel midPanel;
	private JPanel lowPanel;
	private JPanel lowestPanel;

	
	private JSlider speed = new JSlider(300,5000,500);	
	private JCheckBox all = new JCheckBox("Tous");

	private JComboBox period;
	private JComboBox time;
	private JComboBox mode;
	private JComboBox position;
	private JComboBox ascenseurComboBox1;
	private JComboBox ascenseurComboBox2;

	private String[] periodLabels = {"Semaine", "Weekend" };
	private String[] timeLabels = {"Jour", "Nuit" };
	private String[] modeLabels = {"Economie d'énergie", "Economie de déplacement", "Pondération de l'un par l'autre"};
	private String[] positionLabels = {"Repositionnement standard", "Repostionnement par zone","Repositionnement optimisé"};
	private String[] ascenseurLabel = {"Ascenseur1","Ascenseur2","Ascenseur3","Ascenseur4","Ascenseur5","Ascenseur6" };

	private JLabel mainTitle = new JLabel("<html><h1>Mode saisie d'évènements</h1></html>");
	private JLabel subTitle1 = new JLabel("<html><h3><u>Mode de fonctionnement</u></h3></html>");
	private JLabel subTitle2 = new JLabel("<html><h3><u>Appel d'un ascenseur</u></h3></html>");
	private JLabel subTitle3 = new JLabel("<html><h3><u>Déplacement d'un ascenseur</u></h3></html>");
	private JLabel subTitle4 = new JLabel("<html><h3><u>Repositionnement ascenseurs</u></h3></html>");
	private JLabel vide1 = new JLabel("");
	private JLabel vide2 = new JLabel("");
	private JLabel vide3 = new JLabel("");
	private JLabel vide4 = new JLabel("");
	private JLabel vide5 = new JLabel("");
	private JLabel vide6 = new JLabel("");
	private JLabel vide7 = new JLabel("");
	private JLabel vide8 = new JLabel("");
	private JLabel vide9 = new JLabel("");
	private JLabel vide10 = new JLabel("");
	private JTextField callFloor;
	private JTextField destinationFloor;
	private JLabel callFloorLabel = new JLabel("Etage d'appel");
	private JLabel destinationFloorLabel = new JLabel("Etage de destination");
	private JTextField floor;
	
	//Taille texte
	//java.awt.Font fontMainTitle = new java.awt.Font("Serif", Font.PLAIN, 36);
	//mainTitle.setFont(fontMainTitle);
	//mainTitle.setFont(new Font("sansserif", Font.BOLD, 32));


	
	private JButton callButton;
	private JButton sendButton;
	private JButton positionningButton;
	private JButton okButton;
	private JButton positionningBackButton;
	
	
	private JCheckBox circle;

	private JFrame parent;



	
    public EventSimulationFrame(Building b)
    {
		building = b;
		setLayout(new GridLayout(5,0,20,20));

		topestPanel = new JPanel();
		topPanel = new JPanel();
	 	midPanel = new JPanel();
		lowPanel = new JPanel();
		lowestPanel = new JPanel();

		topestPanel.setLayout(new FlowLayout());
		topPanel.setLayout(new GridLayout(4,2,20,4));
		midPanel.setLayout(new GridLayout(4,2,20,5));
		lowPanel.setLayout(new GridLayout(3,2,20,5));
		lowestPanel.setLayout(new GridLayout(3,2,20,5));
		
		getContentPane().add(topestPanel);
		getContentPane().add(topPanel);
		getContentPane().add(midPanel);
		getContentPane().add(lowPanel);
		getContentPane().add(lowestPanel);

		addButtons();
		addEvents();
		setTitle("Mode saisie d'évènements");

		setSize(750,700);
		setVisible(true);
		
    }

		
	public void addButtons()
	{
		
		time = new JComboBox(timeLabels);
		period = new JComboBox(periodLabels);
		mode = new JComboBox(modeLabels);
		position = new JComboBox(positionLabels);
		ascenseurComboBox1 = new JComboBox(ascenseurLabel);
		ascenseurComboBox2 = new JComboBox(ascenseurLabel);
		

		callFloor = new JTextField("5", 3);
		destinationFloor = new JTextField("10",3);
		floor = new JTextField("0",3);

		callButton = new JButton("Appeler");
		sendButton = new JButton("Envoyer");
		positionningButton = new JButton("Repositionner");
		circle = new JCheckBox("Tous",false);
	
		okButton = new JButton("OK");
		positionningBackButton = new JButton("Repositionner");
		speed.createStandardLabels(300);

		topestPanel.add(mainTitle);

		
		topPanel.add(subTitle1);
		topPanel.add(new JLabel(""));
		topPanel.add(time);
		topPanel.add(period);
		topPanel.add(mode);
		topPanel.add(position);
		topPanel.add(new JLabel("Vitesse de la simulation : "));
		topPanel.add(speed);

		midPanel.add(subTitle2);
		midPanel.add(new JLabel(""));
		JLabel callFloorLabel = new JLabel("Etage d'appel :   ");
		callFloorLabel.setHorizontalAlignment(JLabel.CENTER);
		midPanel.add(callFloorLabel);
		midPanel.add(callFloor);
		JLabel destinationFloorLabel = new JLabel("Etage de destination :   ");
		destinationFloorLabel.setHorizontalAlignment(JLabel.CENTER);
		midPanel.add(destinationFloorLabel);
		midPanel.add(destinationFloor);
		midPanel.add(new JLabel(""));
		midPanel.add(callButton);


		lowPanel.add(subTitle3);
		lowPanel.add(new JLabel(""));
		JLabel textAscenseur1 = new JLabel("Ascenseur :   ");
		textAscenseur1.setHorizontalAlignment(JLabel.CENTER);
		lowPanel.add(textAscenseur1);
		lowPanel.add(ascenseurComboBox1);
		lowPanel.add(floor);
		lowPanel.add(sendButton);


		lowestPanel.add(subTitle4);
		lowestPanel.add(new JLabel(""));
		JLabel textAscenseur2 = new JLabel("Ascenseur :   ");
		textAscenseur2.setHorizontalAlignment(JLabel.CENTER);
		lowestPanel.add(textAscenseur2);
		lowestPanel.add(ascenseurComboBox2);
		lowestPanel.add(all);
		lowestPanel.add(positionningBackButton);

	}
		
	public void addEvents()
	{
		okButton.addActionListener(this);
		positionningBackButton.addActionListener(this);
		callButton.addActionListener(this);
		sendButton.addActionListener(this);
		positionningBackButton.addActionListener(this);

		position.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                building.getConfiguration().setReturnMode(position.getSelectedIndex());
            }
        });

		mode.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                building.getConfiguration().setEnergyMode(mode.getSelectedIndex());
            }
        });

		time.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
				boolean value = time.getSelectedIndex() == 0 ? true : false;
                building.getConfiguration().setDay(value);
            }
        });


		period.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
				boolean value = period.getSelectedIndex() == 0 ? true : false;
                building.getConfiguration().setWeek(value);
            }
        });

		speed.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                building.getConfiguration().setRefreshDelay(speed.getValue());
            }
        });

		

	}
	
	public void actionPerformed(ActionEvent ev)
	{
		if(ev.getSource() == callButton)
		{
			if(Integer.parseInt(callFloor.getText()) < -4 || Integer.parseInt(callFloor.getText()) > 40 || Integer.parseInt(destinationFloor.getText()) < -4 || Integer.parseInt(destinationFloor.getText()) > 40)
					return;
			Customer c = new Customer(Integer.parseInt(callFloor.getText())+4 , Integer.parseInt(destinationFloor.getText())+4);
			customers.add(c);
			building.callElevator(c, c.getCurrentFloor(), c.getDestination());
		}

		if(ev.getSource() == sendButton)
		{
			try
			{
				if(Integer.parseInt(floor.getText()) < -4 || Integer.parseInt(floor.getText()) > 40)
					return;
			}
			catch(Exception e)
			{
				return;
			}
			Elevator[] evs = building.getElevators();
			evs[ascenseurComboBox1.getSelectedIndex()].forceToGo(Integer.parseInt(floor.getText(),10) + 4);
		}

		if(ev.getSource() == positionningBackButton)
		{
			building.calculateBackPosition();
			Elevator[] evs = building.getElevators();
			if(!all.isSelected())
				evs[ascenseurComboBox2.getSelectedIndex()].goToOrigin();
			else{
				for(int i = 0; i < 6 ; i++)
				{
					//evs[i].backToOrigin();
					evs[i].goToOrigin();
				}
			}	
		}
		
		//setVisible(false);
	}

	public void stateChanged(ChangeEvent ev)
	{
	
	}

	public void windowClosing(WindowEvent e) {
		add(new Label("fini dans 5 secondes !"));
		pack();
		
		setVisible(false);
		dispose(); 
		System.exit(0); 
  	}
 	public void windowOpened(WindowEvent e) {}
  	public void windowActivated(WindowEvent e) {}
  	public void windowDeactivated(WindowEvent e) {}
 	public void windowClosed(WindowEvent e) {}
  	public void windowIconified(WindowEvent e) {}
 	public void windowDeiconified(WindowEvent e) {}


}
